package com.senriot.quickup.repository

import com.senriot.cloud.common.IRepository
import com.senriot.quickup.domain.Agency
import com.senriot.quickup.domain.QAgency
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer
import org.springframework.data.querydsl.binding.QuerydslBindings
import java.util.*

interface AgencyRepository : IRepository<Agency, Long>, QuerydslBinderCustomizer<QAgency>
{
    @JvmDefault
    override fun customize(bindings: QuerydslBindings, root: QAgency)
    {
        bindings.bind(root.name).first { path, value -> path.containsIgnoreCase(value) }
        bindings.bind(root.createdDate).all { path, value ->
            if (value.size == 1)
            {
                Optional.of(path.eq(value.first()))
            } else
            {
                Optional.of(path.between(value.first().toLocalDate().atStartOfDay(),
                        value.last().toLocalDate().atStartOfDay()))
            }
        }
        bindings.excluding(root.createdBy, root.lastModifiedBy, root.lastModifiedDate)
    }
}
